// pages/content/busiList.js
const app = getApp()
var rsa = require('../../utils/rsa.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startTime:'',
    endTime:'',
    maxTime:'',  //最大值
    dataArr:[],
    canpull:true,
    pageNum:1,
    requestStartTime:'',
    requetEndTime:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getToday();
    this.getDataFromServer();
  },
//搜索
  serchDataAction:function(){
    this.setData({
      canpull: true,
      pageNum: 1,
      dataArr:[],
    })
    this.getDataFromServer();
  },
  //
  
  //请求数据
  getDataFromServer:function(){
    var shopnum = app.globalData.shoopnum;
    const setion = app.globalData.session;

    var stm = this.data.requestStartTime;
    const endtime = this.data.requetEndTime;
    const pagen = this.data.pageNum;
    wx.showLoading({
      title: '刷新中...',
      mask: true,
    })
    rsa.Cpic_fetchPost(rsa.SERVERPORT, { 'method': 'promotion.history', "seller_bn": shopnum, "session_id": setion, "start_time": stm, "end_time": endtime, 'page': pagen, 'page_num': 10, 'desk_no': '', 'mobile': '', 'pay_status': '', 'status': '', 'ship_status': '', 'confirm_status': '', 'pickself_status': '','ship_name':'' }, (err, res) => {
      wx.stopPullDownRefresh()
      wx.hideLoading();
      if (err) {
        wx.showToast({
          title: err,
          icon: 'none',
          duration: 5000,
        })
        return;
      }
      var dlecnt =this.data.dataArr;
      var reqArr = res.data.list;
      var cont = dlecnt.length+reqArr.length;
      var total = parseInt(res.data.total_count) ;
      if (cont>=total){
        this.setData({
          canpull:false,
        })
      }
      dlecnt= dlecnt.concat(reqArr);

      this.setData({
        dataArr: dlecnt,
      })

    })
  },

  datePickerChange2:function(e){
    const date = new Date(Date.parse(e.detail.value));
    var newYear = date.getFullYear();
    var newMonth = date.getMonth() + 1;
    var todayIndex = date.getDate();
    this.setData({
      endTime: newYear + '-' + newMonth + '-' + todayIndex,
      requetEndTime: newYear + '-' + newMonth + '-' + todayIndex+' 24:00:00',
    })
  },
  bindPickerChange:function(e){
    const date = new Date(Date.parse(e.detail.value));
    var newYear = date.getFullYear();
    var newMonth = date.getMonth() + 1;
    var todayIndex = date.getDate();
    this.setData({
      startTime:newYear+'-'+newMonth+'-'+todayIndex,
      requestStartTime: newYear + '-' + newMonth + '-' + todayIndex+' 00:00:00',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

 
  getToday:function(){
    var date = new Date();
    //年  
    var Y = date.getFullYear();
    //月  
    var M = (date.getMonth() + 1 < 10 ? (date.getMonth() + 1) : date.getMonth() + 1);
    //日  
    var D = date.getDate() < 10 ? date.getDate() : date.getDate();

    var h = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var m = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    this.setData({
      startTime: Y + '-' + M + '-' + (D),
      maxTime: Y + '-' + M + '-' + D,
      endTime: Y + '-' + M + '-' + D,
      requestStartTime: Y + '-' + M + '-' + (D)+' 00:00:00',
      requetEndTime: Y + '-' + M + '-' + D +' '+h+':'+m+':'+s,
    })
  },
  
  // 下拉刷新  
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
  },
  // 上拉加载  
  onReachBottom(e) {
    let that = this;
    if (that.data.canpull) {
      that.setData({
        pageNum: that.data.pageNum + 1,  // 每次触发上拉事件，把pageNum+1  
      });

      that.getDataFromServer();
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})